<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { subFormSchema } from './dict.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { saveOrUpdateDictItem } from './dict.api';

  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 100,
    schemas: subFormSchema,
    showActionButtonGroup: false,
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    const valueType = data.record.valueType;
    if (valueType === 'number') {
      updateSchema({
        field: 'value',
        component: 'InputNumber',
        defaultValue: 0,
      });
    } else {
      updateSchema({
        field: 'value',
        component: 'Input',
        defaultValue: '',
      });
    }

    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    } else {
      setFieldsValue({
        dictCode: data.record.dictCode,
        valueType: valueType,
      });
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增字典项' : '编辑字典项'));

  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({ confirmLoading: true });
      await saveOrUpdateDictItem(values, isUpdate.value);
      closeDrawer();
      emit('success');
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
